// src/api/heritage.js
import api from '@/api/user'

// 分页获取文化遗产列表（包括搜索功能）
export const findHeritagePage = (params) => {
    // 构造搜索参数
    const searchParams = {
        current: params.current,
        size: params.size
    };

    // 只有当参数存在且不为空时才添加到请求参数中
    if (params.categoryId !== undefined && params.categoryId !== null && params.categoryId !== '') {
        searchParams.categoryId = params.categoryId;
    }
    if (params.name !== undefined && params.name !== null && params.name.trim() !== '') {
        searchParams.name = params.name.trim();
    }

    return api.get('/heritage/search', { params: searchParams });
}


// 根据ID获取文化遗产详情
export const getHeritageById = (id) => {
    return api.get(`/heritage/${id}`)
}

// 新增文化遗产
export const addHeritage = (data) => {
    return api.post('/heritage', data)
}

// 更新文化遗产
export const updateHeritage = (data) => {
    return api.put('/heritage', data)
}

// 删除文化遗产
export const deleteHeritage = (id) => {
    return api.delete(`/heritage/${id}`)
}

// 增加浏览次数
export const increaseViewCount = (id) => {
    return api.put(`/heritage/view/${id}`)
}

// 点赞文化遗产
export const likeHeritage = (id) => {
    return api.put(`/heritage/like/${id}`)
}

// 取消点赞
export const unlikeHeritage = (id) => {
    return api.put(`/heritage/unlike/${id}`)
}

// 收藏文化遗产
export const collectHeritage = (id) => {
    return api.put(`/heritage/collect/${id}`)
}

// 取消收藏
export const uncollectHeritage = (id) => {
    return api.put(`/heritage/uncollect/${id}`)
}


// 上传文化遗产图片
export const uploadHeritageImage = (file) => {
    const formData = new FormData();
    formData.append('file', file);
    return api.post('/category/upload', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
}

export default {
    findHeritagePage,
    getHeritageById,
    addHeritage,
    updateHeritage,
    deleteHeritage,
    increaseViewCount,
    likeHeritage,
    unlikeHeritage,
    collectHeritage,
    uncollectHeritage,
    uploadHeritageImage
}
